
Imports cmplib01

'// purpose:
'   show all resources generated, stored and consumed by the faction
'   show all resources constantly consumed by the faction's installations
'   allow the deactivation of constant rules so that they don't consume resources

'TASK: show all resources generated/stored by the faction
'TASK: show all resources that are constantly consumed
'TASK: allow rules to be activated and deactivated (non-combat rules only)

Public Class frmResourceManager

    Private ptrGame As CampaignerGameData
    Private resourceListing As New Collection '// we'll use a key in this one


    Public Shadows Sub ShowDialog(ByRef game As CampaignerGameData)
        ptrGame = game

        '// populate it boss.
        Populate()
        MyBase.ShowDialog()
    End Sub

    Private Sub Populate()

        resourceListing.Clear()

        Try

            BuildResourceList()
            CalculateConstantConsumption()
            ListResources()

        Catch ex As Exception
            HandleException("frmResourceManager.Populate", ex)
        End Try

    End Sub


    'Private Function ShowResourceStores(ByVal name As String) As Long()
    '    '// returns long(2): element 0 = total amount, element 1 = generate amount, element 2 = constant consume
    '    '// returns the amount of the provided resource name the selected faction has
    '    Try
    '        Dim result() As Long = {0, 0, 0}

    '        For Each mem As ChitMember In ptrGame.Chits
    '            If mem.OwningFaction.Equals(ptrGame.selectedFaction) Then
    '                If mem.UnitType = GroundChitClass_cgd.Objective Then
    '                    '// if this guy generates the resource we're looking for, add it. Yah.
    '                    For Each poo As ResourcePoolItem In mem.Resources
    '                        If poo.resourceTypeptr.ResourceName = name Then
    '                            '// it does! yes!!
    '                            result(0) += poo.localAmount
    '                            result(1) += poo.accrueAmount
    '                        End If
    '                    Next
    '                End If
    '                '// check here for consumption... even UNIT TYPES can have rules that constantly consume resources...
    '                For Each rul As Rule In mem.chitRules
    '                    If TypeOf rul.ptrRule Is ResourceConsumingRuleType Then
    '                        '// yeah, it totally consumes a resource.
    '                        If rul.Active = True Then
    '                            '// add it only if active
    '                            For Each rescost As ResourceCost In DirectCast(rul.ptrRule, ResourceConsumingRuleType).Costs
    '                                If rescost.ResourceTypeName = name Then
    '                                    result(2) += rescost.ResourceCost
    '                                End If
    '                            Next
    '                        End If
    '                    End If
    '                Next
    '            End If
    '        Next

    '    Catch ex As Exception
    '        HandleException("frmResourceManager.ShowResourceStores", ex)
    '    End Try

    'End Function


    '/////// old stuff?.....

    Private Sub BuildResourceList()
        Try
            Dim fact As Faction = ptrGame.selectedFaction

            '// we'll build the whole resource list here: Even if the faction doesn't produce any of a specific type of resource, it will be listed. :D
            For Each key As String In ptrGame.ResourceTypes_g.HT.Keys
                Dim tp As New consumptionResource
                tp.resourceTypeptr = ptrGame.ResourceTypes_g.Item(key)
                resourceListing.Add(tp, tp.resourceTypeptr.ResourceName)
            Next

            '// put the resource values in for each of the resources each installation generates
            For Each mem As ChitMember In ptrGame.Chits
                If mem.UnitType = GroundChitClass_cgd.Objective Then
                    If mem.OwningFaction.Equals(fact) Then
                        For Each rp As ResourcePoolItem In DirectCast(mem, InstallationChit).Resources
                            Dim targetPoolItem As consumptionResource = resourceListing.Item(rp.resourceTypeptr.ResourceName)

                            '// increase the amount of this resource
                            With targetPoolItem
                                .accrueAmount += rp.accrueAmount
                                .localAmount += rp.localAmount
                                .maxAmount += rp.maxAmount
                            End With

                        Next

                    End If
                End If
            Next
        Catch ex As Exception
            HandleException("frmResourceManager.BuildResourceList", ex)
        End Try
    End Sub

    Private Sub CalculateConstantConsumption()
        Try
            '// this is ONLY constant consumption, not the total amount that COULD be consumption-ed... :S
            For Each mem As ChitMember In ptrGame.Chits
                For Each ru As Rule In mem.chitRules
                    If ru.ptrRule.AppliesDuring = RuleApplies_cgd.Constant Then
                        If TypeOf ru.ptrRule Is ResourceConsumingRuleType Then
                            Dim rcrt As ResourceConsumingRuleType = ru.ptrRule
                            For Each resCost As ResourceCost In rcrt.Costs
                                '// find the resource in our list and bump up the the consumption cost property
                                Dim cs As consumptionResource = resourceListing.Item(resCost.ResourceTypeName)
                                cs.consumption += resCost.ResourceCost
                            Next
                        End If
                    End If
                Next
            Next

        Catch ex As Exception
            HandleException("frmResourceManager.CalculateConstantConsumption", ex)
        End Try
    End Sub

    Private Sub ListResources()
        lvwConsumption.Items.Clear()
        Try
            For Each cs As consumptionResource In resourceListing
                Dim lvwitem As New ListViewItem
                lvwitem.Text = cs.resourceTypeptr.ResourceName & " +" & cs.accrueAmount & "/" & cs.localAmount & "/-" & cs.consumption
                If cs.consumption > cs.accrueAmount Then
                    lvwitem.ForeColor = Color.DarkRed
                Else
                    lvwitem.ForeColor = Color.Black
                End If
                lvwitem.Tag = cs
                lvwConsumption.Items.Add(lvwitem)
            Next
        Catch ex As Exception
            HandleException("frmResourceManager.ListResources", ex)
        End Try
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.Hide()
    End Sub

    Private Sub lvwConsumption_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwConsumption.SelectedIndexChanged
        lvwInstallations.Items.Clear()
        If lvwConsumption.SelectedItems.Count = 0 Then Exit Sub

        ListInstallations()

    End Sub

    Private Sub ListInstallations()
        Try
            Dim consRes As consumptionResource = lvwConsumption.SelectedItems(0).Tag
            lvwInstallations.Items.Clear()

            '// with a resource type selected, we'll go through each installation and rule that consumes the selected resource. This only lists "constant" rules.
            For Each mem As ChitMember In ptrGame.Chits
                If mem.OwningFaction.Equals(ptrGame.selectedFaction) Then
                    If mem.UnitType = GroundChitClass_cgd.Objective Then
                        For Each ru As Rule In mem.chitRules
                            If TypeOf ru.ptrRule Is ResourceConsumingRuleType Then
                                For Each co As ResourceCost In DirectCast(ru.ptrRule, ResourceConsumingRuleType).Costs
                                    If co.ResourceTypeName = consRes.resourceTypeptr.ResourceName Then
                                        '// whoohoo! We gots us a winner! Make an entry
                                        '// installation, rule, resource, active. A link to the actual rule is in the TAGGGGG
                                        Dim lvw As New ListViewItem
                                        lvw.Text = mem.Name
                                        lvw.SubItems.Add(ru.ptrRule.RuleName)
                                        lvw.SubItems.Add(co.ResourceCost.ToString)
                                        lvw.Tag = ru
                                        If ru.Active = True Then
                                            lvw.SubItems.Add("Yes")
                                            lvw.ForeColor = Color.Black
                                        Else
                                            lvw.SubItems.Add("No")
                                            lvw.ForeColor = Color.DarkGray
                                        End If
                                        lvwInstallations.Items.Add(lvw)
                                    End If
                                Next
                            End If
                        Next
                    End If
                End If
            Next
        Catch ex As Exception
            HandleException("frmResourceManager.ListInstallation", ex)
        End Try
    End Sub

    Private Sub frmResourceManager_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Dim colLength As Integer = (lvwInstallations.Size.Width - 20) / 4
        For Each colHead As ColumnHeader In lvwInstallations.Columns
            colHead.Width = colLength
        Next

    End Sub

    Private Sub lvwInstallations_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvwInstallations.MouseUp
        If lvwInstallations.SelectedItems.Count = 0 Then Exit Sub

        If e.Button = Windows.Forms.MouseButtons.Right Then
            mnupopup.Show(lvwInstallations, e.X, e.Y)
        End If
    End Sub

    Private Sub mnuActive_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuActive.Click
        Try
            Dim ru As Rule = lvwInstallations.SelectedItems.Item(0).Tag
            ru.Active = True
            ListInstallations()
        Catch ex As Exception
            HandleException("frmResourceManager.mnuActive_click", ex)
        End Try
    End Sub

    Private Sub mnuInactive_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuInactive.Click
        Try
            Dim ru As Rule = lvwInstallations.SelectedItems.Item(0).Tag
            ru.Active = False
            ListInstallations()
        Catch ex As Exception
            HandleException("frmResourceManager.mnuInactive_click", ex)
        End Try
    End Sub
End Class


Public Class consumptionResource : Inherits ResourcePoolitem
    Private _consumption As Long

    Public Property consumption() As Long
        Get
            Return _consumption
        End Get
        Set(ByVal value As Long)
            _consumption = value
        End Set
    End Property
End Class